from django.shortcuts import render
from rest_framework.generics import ListAPIView, UpdateAPIView, CreateAPIView, RetrieveAPIView
from rest_framework.response import Response
from .serializers import *


# Create your views here.


# 展示推荐职位
# 获取推荐的招聘信息列表
# GET  Path： /recruits/search/recommend/
class RecommendListView(ListAPIView):
    serializer_class = RecommendModelSerializer
    queryset = Recruit.objects.all()[0:4]


# 最新职位
# 获取最新的招聘信息列表
# GET Path： /recruits/search/latest/
class LatestJobListView(ListAPIView):
    serializer_class = RecommendModelSerializer
    queryset = Recruit.objects.all()

    def get_queryset(self):
        # 创建时间排序
        return self.queryset.order_by('-createtime')[0:4]


# 热门企业
# 获取热门企业列表
# GET Path： /enterprise/search/hotlist/
class HotEnterpiseListView(ListAPIView, CreateAPIView):
    serializer_class = EnterpriseSimpleModelserializer
    queryset = Enterprise.objects.all()

    def get_queryset(self):
        # 根据浏览量区分热门企业
        return self.queryset.order_by('-visits')

    # 收藏公司
    # 收藏公司或者取消收藏公司，后台会自行动态判断
    # POST Path： /enterprise/{id}/collect/
    def create(self, request, *args, **kwargs):
        user = self.request.user
        if user.is_authenticated:
            enterprise = self.get_object()
            users = enterprise.users.all()
            if user in users:
                enterprise.users.remove(user)
                enterprise.save()
                return Response({
                    'message': '取消收藏成功',
                    'success': "true"
                })
            else:
                enterprise.users.add(user)
                enterprise.save()
                return Response({
                    'message': '收藏成功',
                    'success': "true"
                })
        return Response({
            'message': '请先登录',
            'success': "false"
        })


# 热门城市
# 获取热门城市
# GET Path： /city/hotlist/
class HotCityListView(ListAPIView):
    serializer_class = HotCityModelSerializer
    queryset = City.objects.filter(ishot=1)


from rest_framework.generics import GenericAPIView


# 搜索职位
# 搜索某个职位
# POST Path： /recruits/search/city/keyword/
class SearchJobsListView(CreateAPIView, ListAPIView):
    serializer_class = RecommendModelSerializer
    queryset = Recruit.objects.all()

    # 搜索职位
    def get_queryset(self):
        name = self.request.data.get('cityname')
        keyword = self.request.data.get('keyword')
        return self.queryset.filter(city=name, jobname__contains=keyword)

    def create(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)


# 职位详情
# 获取详情信息
# GET Path： /recruits/{id}/
class JobDetailListView(RetrieveAPIView):
    serializer_class = RecommendDetailModelSerializer
    queryset = Recruit.objects.all()

    def get_queryset(self):
        pk = self.kwargs.get('pk')
        return self.queryset.filter(pk=pk)


# 增加职位访问次数
# 增加某个职位的访问量
# PUT Path： /recruits/{id}/visit/
class JobVisitsUpdateView(UpdateAPIView, CreateAPIView):
    serializer_class = RecommendModelSerializer
    queryset = Recruit.objects.all()

    # 重写update方法
    def update(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.visits += 1
        instance.save()
        return Response({
            'message': '更新成功',
            'success': "true"
        })

    # 收藏职位
    # 收藏或者取消收藏职位
    # POST Path： /recruits/{id}/collect/
    def create(self, request, *args, **kwargs):
        user = self.request.user
        if user.is_authenticated:
            recruits = self.get_object()
            # ManyToMany字段,插入中间表,判断用户工作收藏表中是否包含用户
            # 包含则表示已收藏,点击则取消收藏
            if user in recruits.users.all():
                recruits.users.remove(user)
                recruits.save()
                return Response({
                    'message': '取消收藏成功',
                    'success': "true"
                })
            else:
                recruits.users.add(user)
                recruits.save()
                return Response({
                    'message': '收藏成功',
                    'success': "true"
                })
        return Response({
            'message': '请先登录',
            'success': "false"
        })


# 企业详情
# 获取企业详情信息
# GET Path： /enterprise/{id}/
class EnterpriseDetailListView(RetrieveAPIView, UpdateAPIView):
    serializer_class = EnterpriseAllModelserializer
    queryset = Enterprise.objects.all()

    # 增加企业访问次数
    # PUT Path： /enterprise/{id}/visit/
    def update(self, request, *args, **kwargs):
        enterprise = self.get_object()
        enterprise.visits += 1
        enterprise.save()
        return Response({
            'message': '更新成功',
            'success': "true"
        })
